package com.sig.models;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import android.util.Log;

public class Triangle{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Les points du triangles
	 */	
	private final Point p1;
	private final Point p2;
	private final Point p3;
	
	/**
     * Vertex buffer.
     */
    private FloatBuffer vertexBuffer;

   
    /**
     * Index buffer.
     */
    private ShortBuffer indexBuffer;
    
    /**
     * byte buffer
     */
    private ByteBuffer bbuffer;

	public Triangle(Point p1, Point p2, Point p3) {
		super();
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
		manageVertexBuffer(p1, p2, p3);
		manageIndexBuffer();
	}
	
	
	
    
	/**
	 * @return the p1
	 */
	public Point getP1() {
		return p1;
	}




	/**
	 * @return the p2
	 */
	public Point getP2() {
		return p2;
	}




	/**
	 * @return the p3
	 */
	public Point getP3() {
		return p3;
	}




	/**
	 * @return the vertexBuffer
	 */
	public FloatBuffer getVertexBuffer() {
		return vertexBuffer;
	}




	/**
	 * @return the indexBuffer
	 */
	public ShortBuffer getIndexBuffer() {
		return indexBuffer;
	}




	/**
	 * create vertex buffer
	 */
	private void manageVertexBuffer(Point p1,Point p2,Point p3){
		bbuffer = ByteBuffer.allocateDirect(36);
		bbuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = bbuffer.asFloatBuffer();
        vertexBuffer.put(new float[] {
        								p1.getX(),p1.getY(),0,
        								p2.getX(),p2.getY(),0,
        								p3.getX(),p2.getY(),0
        });
        Log.d("triangles", p1.getX()+","+p2.getX());
        vertexBuffer.position(0);
	}
	
	
	/**
	 * create index buffer
	 */
	private void manageIndexBuffer(){
		bbuffer = ByteBuffer.allocateDirect(6);
		bbuffer.order(ByteOrder.nativeOrder());
        indexBuffer = bbuffer.asShortBuffer();
        indexBuffer.put(new short[] { 0, 1, 2 });
        indexBuffer.position(0);
	}

}
